package chat.chit.tit.mit;

import java.util.ArrayList;
import java.util.List;

import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.StringUtils;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import chat.chit.tit.mit.adapter.ChatAdapter;
import chat.chit.tit.mit.model.ChatContentEntity;
import chat.chit.tit.mit.variables.Variables;

@SuppressLint("ValidFragment")
public class ChatContentFragment extends Fragment {

	private ListView lvContent;
	private ChatAdapter contentAdapter;
	private List<ChatContentEntity> listContent;
	private ImageView btnEnter;
	private EditText editEnter;
	private XMPPConnection conn = Variables.connection;
	private boolean isChatContinue = false;
	private String name;
	private Handler mHandler = new Handler();

	@SuppressLint("ValidFragment")
	public ChatContentFragment(String name) {
		this.name = name;
		listContent = new ArrayList<ChatContentEntity>();
		getMessage(conn);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.chat_content_fragment,
				container, false);
		btnEnter = (ImageView) rootView.findViewById(R.id.btn_enter);
		editEnter = (EditText) rootView.findViewById(R.id.edit_enter);
		lvContent = (ListView) rootView.findViewById(R.id.list_contact_search);
		contentAdapter = new ChatAdapter(getActivity(), listContent);
		lvContent.setAdapter(contentAdapter);
		btnEnter.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!editEnter.getText().toString().trim().equals("")) {
					if (isChatContinue) {
						String str = listContent.get(listContent.size() - 1)
								.getContent()
								+ System.getProperty("line.separator")
								+ editEnter.getText().toString().trim();
						listContent.get(listContent.size() - 1).setContent(str);
						sendMessage();
						editEnter.setText("");
						contentAdapter.notifyDataSetChanged();
					} else {
						ChatContentEntity content = new ChatContentEntity("me",
								editEnter.getText().toString().trim(), true);
						listContent.add(content);
						sendMessage();
						editEnter.setText("");
						contentAdapter.notifyDataSetChanged();
						isChatContinue = true;
					}

				}
			}
		});
		return rootView;
	}

	public void sendMessage() {
		// String name = listContent.get(listContent.size()-1).getFullName();
		Message msg = new Message(name, Message.Type.chat);
		msg.setBody(editEnter.getText().toString().trim());
		if (conn != null) {
			conn.sendPacket(msg);
		}
	}

	public void getMessage(XMPPConnection connection) {
		this.conn = connection;
		if (connection != null) {
			// Add a packet listener to get messages sent to us
			PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
			connection.addPacketListener(new PacketListener() {
				@Override
				public void processPacket(Packet packet) {
					Message message = (Message) packet;
					if (message.getBody() != null) {
						String fromName = StringUtils.parseBareAddress(message
								.getFrom());
						Log.i("XMPPChatDemoActivity", "Text Recieved "
								+ message.getBody() + " from " + fromName);
						if (fromName.equals(name)) {
							ChatContentEntity content = new ChatContentEntity(
									name, message.getBody(), true);
							listContent.add(content);

							isChatContinue = false;
						}
						mHandler.post(new Runnable() {
							public void run() {
								contentAdapter.notifyDataSetChanged();
							}
						});
					}
				}
			}, filter);
		}
	}
}
